import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

const activityNameObj = {
  "1": "砍价",
  "2": "拼团",
  "3": "一元拍卖"
}

class orderGoods extends Base {
  constructor(goods, order) {
    super();

    this.order = order;

    this.orderNoSub = goods.orderNoSub;
    this.defId = goods.goodsDefId;
    this.modelId = goods.goodsModelId;
    this.name = goods.goodsName;
    this.modelName = goods.goodsModelName;
    this.price = goods.goodsModelSalePrice;
    this.sourcePrice = goods.goodsModelSourcePrice;
    this.count = goods.buyCount;
    this.payPrice = goods.shouldPayPrice;
    this.buyMethod = goods.buyMethod;
    this.orderRefuseApplyId = goods.orderRefuseApplyId;
    this.status = goods.status;

    this.setDatetimeData(goods, ['payTime', 'cancelTime', 'refuseTime']);

    let refusePrice = goods.refusePrice;
    if (SystemUtil.isEmpty(refusePrice)) {
      refusePrice = 0;
    } else {
      refusePrice = _.round(refusePrice / 100, 2);
    }
    this.refusePrice = refusePrice;
  }
}

export default class OrderRecord extends Base {
  constructor(goodsList) {
    super();

    let obj = goodsList[0];

    let fields = ['branchId', 'partnerId', 'branchName', 'partnerName', 'buyerName', 'buyerAddress', 'buyerPhone'];
    this.initObject(obj, fields);

    let orderId = obj.orderNoMain;
    this.orderId = orderId;

    let preferentialPrice = obj.preferentialPrice;
    if (SystemUtil.isEmpty(preferentialPrice)) {
      preferentialPrice = 0;
    }
    this.preferentialPrice = preferentialPrice;

    this.setDatetimeData(obj, ['createTime', 'payTime', 'cancelTime', 'refuseTime']);

    let type = obj.sourceSub;
    this.type = type;
    let activityTypeId = obj.activityTypeId;
    this.activityTypeId = activityTypeId;
    if (type === 'YDDD') {
      let activityName = activityNameObj[activityTypeId];
      if (activityName === undefined) {
        this.activityName = `${obj.goodsName} 预付款`;
      } else {
        this.activityName = `${activityName}-${obj.goodsName} 预付款`;
      }
    } else if (type === 'NOGOODS') {
      this.activityName = '门店支付';
    }

    if (['YDDD', 'NOGOODS'].indexOf(type) !== -1) {
      this.goodsList = null;
      this.status = obj.status;
      this.orderNoSub = obj.orderNoSub;
      let payPrice = obj.shouldPayPrice;
      this.payPrice = payPrice;
      this.totalPrice = _.round(payPrice - this.preferentialPrice, 2);
      this.buyerPhone = obj.orderMakerPhoneNum;

      let refusePrice = obj.refusePrice;
      if (SystemUtil.isEmpty(refusePrice)) {
        refusePrice = 0;
      } else {
        refusePrice = _.round(refusePrice / 100, 2);
      }
      this.refusePrice = refusePrice;
      return;
    }

    this.setGoodsList(goodsList);
  }

  setGoodsList(goodsData) {
    let totalPrice = 0,
      refusePrice = 0;
    let goodsList = [];
    let statusCount = {
      'status_1': 0,
      'status_2': 0,
      'status_3': 0
    };
    for (let i = 0; i < goodsData.length; i++) {
      let goods = goodsData[i];
      goods = new orderGoods(goods, this);

      let status = goods.status;
      if ([1, 2, 3].indexOf(status) !== -1) {
        statusCount[`status_${status}`]++;
      }

      totalPrice += goods.payPrice;
      refusePrice += goods.refusePrice;
      goodsList.push(goods);
    }

    this.goodsList = goodsList;
    this.totalPrice = _.round(totalPrice - this.preferentialPrice, 2);
    this.refusePrice = _.round(refusePrice, 2);

    if (goodsList.length === 1) {
      this.status = goodsList[0].status;
      this.isSame = true;
      return;
    }

    let count = 0;
    for (const key in statusCount) {
      if (statusCount[key] !== 0) {
        count++;
      }
    }

    if (count > 1) {
      this.status = 1;
      this.isSame = false;
    } else {
      this.status = goodsList[0].status;
      this.isSame = true;
    }
  }

  getStatPrice() {
    let refundPrice = 0,
      turnoverPrice = 0;

    let goodsList = this.goodsList;
    if (goodsList === null) {
      let status = this.status;
      if (status === 3) {
        refundPrice += this.refusePrice;
      } else if ([1, 2, 4].indexOf(status)) {
        turnoverPrice += this.payPrice;
      }
    } else {
      for (let i = 0; i < goodsList.length; i++) {
        const goods = goodsList[i];
        let status = goods.status;
        if (status === 3) {
          refundPrice += goods.refusePrice;
        } else if ([1, 2, 4].indexOf(status)) {
          turnoverPrice += goods.payPrice;
        }
      }
    }

    return {
      refundPrice: _.round(refundPrice, 2),
      turnoverPrice: _.round(turnoverPrice, 2)
    }
  }
}
